<?php

class WaitersController extends AppController {

    var $name = 'Waiters';
    var $uses = array();
    var $components = array('WritingPad');

    /**
     * When on Menu Card screen, user selects the items by checking the checkboxes and
     * then clicks on Add to Order, all the items get added at once in the order.
     */
    function takeOrder() {
        $ordered_items = $this->data["MenuItem"];
        //$uniqueOrderToken = $this->data["MenuItem"]["order_token"];
        $url = "menu_items/getUniqueOrderToken/";
        $uniqueOrderToken = $this->requestAction($url);


        $output = "";
        foreach ($ordered_items as $key => $value) {
            /**
             * see which items have been ordered.
             * value = 1 indicates the checkbox was checked.
             * That means the item was selected.
             */
            if (!empty($value) && $value == 1) {
                $output = $output . $key . ",";
                /**
                 * Write the Menu_Item_Id on writing pad.
                 */
                $this->WritingPad->noteDown($uniqueOrderToken, $key);
            }
        }
        //$output = $this->WritingPad->readPad($uniqueOrderToken);//$output . " - [" . $uniqueOrderToken . "]";
        $output = $this->WritingPad->getHumanReadableOrder($uniqueOrderToken);

        $this->set("output", $output);
        $this->set("order_token", $uniqueOrderToken);
        //$this->render('ajx_success', 'ajax');
        $this->render('temp_bill', 'ajax');
    }

    /**
     * When the hyperlink on Menu Card is clicked, this function is called.
     * This function simply works similar to takeOrder() function.
     *
     * Except that, this function can take only one item at a time.
     * If bulk order is to be placed, takeOrder() function is to be used.
     * 
     * @param <type> $menu_item_id
     */
    function takeOrder_2($menu_item_id) {

        $output = "";
        $url = "menu_items/getUniqueOrderToken/";
        $uniqueOrderToken = $this->requestAction($url);

        /**
         * Write the Menu_Item_Id on writing pad.
         */
        $this->WritingPad->noteDown($uniqueOrderToken, $menu_item_id);


        //$output = $this->WritingPad->readPad($uniqueOrderToken);//$output . " - [" . $uniqueOrderToken . "]";
        $output = $this->WritingPad->getHumanReadableOrder($uniqueOrderToken);

        $this->set("output", $output);
        $this->set("order_token", $uniqueOrderToken);
        $this->render('temp_bill', 'ajax');
    }

    function updateOrder() {
        //debug($this->params);
        /**
         * $this->params['form'] contains the itemId and new quantity.
         */
        $itemToUpdateQuantityOf = $this->params['form'];
        //debug($itemToUpdateQuantityOf);
        foreach ($itemToUpdateQuantityOf as $menu_item_id => $newQty) {
            if (is_numeric($newQty) && abs($newQty)==$newQty) {
                $newOrder = $this->updateQuantityOfItemInOrder($menu_item_id, $newQty);
            } else {
                $url = "menu_items/getUniqueOrderToken/";
                $uniqueOrderToken = $this->requestAction($url);
                $newOrder = $this->WritingPad->getHumanReadableOrder($uniqueOrderToken);
            }
        }
        $ordered_items_list = $newOrder;

        //If there is at least 1 item in the order, show the order, else show the menu card.
//        if (is_array($ordered_items_list) && count($ordered_items_list) > 0) {
        $this->set("ordered_items_list", $ordered_items_list);
        $this->render('update_qty', 'ajax');
//        } else {
//            $this->Session->setFlash("Please choose delicious items from the menu!");
//            $url = "/menu_items/showmenu/";
//            $this->redirect($url);
//            return;
//        }
    }

    function updateQuantityOfItemInOrder($menu_item_id, $newQty) {

        $ordered_items = $this->data["MenuItem"];
        //$uniqueOrderToken = $this->data["MenuItem"]["order_token"];
        $url = "menu_items/getUniqueOrderToken/";
        $uniqueOrderToken = $this->requestAction($url);

//        debug($uniqueOrderToken);
//        debug($menu_item_id);
//        debug($newQty);
        if ($newQty > 0) {
            $this->WritingPad->updateQuantity($uniqueOrderToken, $menu_item_id, $newQty);
        } else {
            $this->WritingPad->removeFromPad($uniqueOrderToken, $menu_item_id);
        }
        //$output = $this->WritingPad->readPad($uniqueOrderToken);//$output . " - [" . $uniqueOrderToken . "]";
        $output = $this->WritingPad->getHumanReadableOrder($uniqueOrderToken);

//        $this->set("output", $output);
//        $this->set("order_token", $uniqueOrderToken);
//        //$this->render('ajx_success', 'ajax');
//        $this->render('temp_bill', 'ajax');

        return $output;
    }

    function addToOrder() {

        $this->Session->activate();
        $this->Session->write('some.key', 'some.value');
        $this->Session->setFlash('Order placed in addToORder() method');
    }

    /**
     * @author: Meshari
     * This function handles removal of the item from order.
     * It is ajax function.
     *
     * @param <type> $itemIdToRemove
     */
    function removeItemFromOrder($itemIdToRemove=null) {

        $url = "menu_items/getUniqueOrderToken/";
        $uniqueOrderToken = $this->requestAction($url);

        if (isset($itemIdToRemove) && !empty($itemIdToRemove) && $itemIdToRemove != null) {
            $this->WritingPad->removeFromPad($uniqueOrderToken, $itemIdToRemove);

            //read the updated order
            $output = $this->WritingPad->getHumanReadableOrder($uniqueOrderToken);

            $this->set("output", $output);
            $this->set("order_token", $uniqueOrderToken);
            //$this->render('ajx_success', 'ajax');
            $this->render('temp_bill', 'ajax');
        }
    }

    function ajxOrder() {
//         debug($this->data);
        if (!empty($this->data)) {
            $this->takeOrder();
        } else {

            $output = "Take your time to choose from the menu.";
            $this->set("output", $output);
            /**
             * $this->render( argument_1 , 'ajax');
             * here argument_1 = name of ctp file in views of waiters controller.
             * So in the following statement ajx_failed is nothing but
             * ajx_failed.ctp in waiters_controller views.
             */
            $this->render('ajx_failed', 'ajax');
        }
    }

    function cancelOrder($order_token=null) {
        //e($order_token);
        if (!empty($order_token)) {
            e("Order cancelled.");
            $uniqueOrderToken = $order_token; //$this->data["MenuItem"]["order_token"];
            $this->WritingPad->clearPad($uniqueOrderToken);

            //$output = "Order cancelled.";
            //$this->set("output", $output);
            $this->set("order_token", $uniqueOrderToken);
            $this->render('temp_bill', 'ajax');
        } else {
            e("Nothing to cancle.");
            exit();
            $output = "Order was not cancelled";
            $this->set("output", $output);
            $this->render('temp_bill', 'ajax');
        }
    }

    function startPreparingTheOrder() {

        echo "going to prepare the order.";

        /**
         * We also need to clear the pad. i.e. Session
         */
        $url = "menu_items/getUniqueOrderToken/";
        $uniqueOrderToken = $this->requestAction($url);

        $this->WritingPad->clearPad($uniqueOrderToken);
        return;
    }

}

?>
